package com.cjlgb.design.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cjlgb.design.common.core.bean.ResultBean;
import com.cjlgb.design.common.core.util.TreeUtil;
import com.cjlgb.design.common.security.util.SpringSecurityUtil;
import com.cjlgb.design.upms.api.entity.SysMenu;
import com.cjlgb.design.upms.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author WFT
 * @date 2019/8/15
 * description: 后台菜单控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/sys/menu")
public class SysMenuController {

    private final SysMenuService menuService;

    /**
     * 获取当前用户的权限菜单
     * @return ResultBean<List<SysMenu>>
     */
    @GetMapping(value = "/getAuthorities")
    public ResultBean<List<SysMenu>> getAuthorities(){
        Collection<String> authorities = SpringSecurityUtil.getAuthorities();
        if (authorities.isEmpty()){
            return ResultBean.ok(new ArrayList<>(0));
        }
        List<SysMenu> list = this.menuService.list(
                Wrappers.<SysMenu>lambdaQuery()
                        .notIn(SysMenu::getType,3)
                        .in(SysMenu::getPermission, SpringSecurityUtil.getAuthorities())
        );
        return ResultBean.ok(TreeUtil.build(list));
    }

    /**
     * 获取菜单树
     * @return ResultBean<List<SysMenu>>
     */
    @GetMapping(value = "/tree")
    @PreAuthorize(value = "@pms.hasPermission('permission.menu.query','permission.role.authorities')")
    public ResultBean<List<SysMenu>> getMenuTree(){
        List<SysMenu> list = this.menuService.list();
        return ResultBean.ok(TreeUtil.build(list));
    }

    /**
     * 根据菜单Id获取菜单信息
     * @param id 菜单Id
     * @return ResultBean<SysMenu>
     */
    @GetMapping(value = "/{menuId}")
    public ResultBean<SysMenu> getMenuById(@PathVariable(value = "menuId") Long id){
        SysMenu menu = this.menuService.getById(id);
        Assert.notNull(menu,"菜单Id不存在");
        return ResultBean.ok(menu);
    }

    /**
     * 添加菜单
     * @param menu 菜单信息
     * @return ResultBean
     */
    @PostMapping
    @PreAuthorize(value = "@pms.hasPermission('permission.menu.add')")
    public ResultBean addMenu(@RequestBody SysMenu menu){
        Assert.notNull(menu.getPid(),"父级Id不能为空");
        Assert.hasText(menu.getName(),"名称不能为空");
        Assert.hasText(menu.getPermission(),"权限标识不能为空");
        //  保存菜单
        return this.menuService.createMenu(menu);
    }

    /**
     * 修改菜单
     * @param menu 菜单信息
     * @return ResultBean
     */
    @PutMapping
    @PreAuthorize(value = "@pms.hasPermission('permission.menu.edit')")
    public ResultBean editMenu(@RequestBody SysMenu menu){
        Assert.notNull(menu.getId(),"Id不能为空");
        Assert.notNull(menu.getSort(),"排序值不能为空");
        Assert.hasText(menu.getName(),"名称不能为空");
        Assert.hasText(menu.getPermission(),"权限标识不能为空");
        //  保存菜单
        return this.menuService.editMenu(menu);
    }

    /**
     * 删除菜单
     * @param id 菜单Id
     * @return ResultBean
     */
    @DeleteMapping(value = "/{id}")
    @PreAuthorize(value = "@pms.hasPermission('permission.menu.delete')")
    public ResultBean deleteMenu(@PathVariable(value = "id") Long id){
        this.menuService.deleteById(id);
        return ResultBean.ok();
    }

}
